const axios = require('axios')
//添加访问前缀 配合webpackDevServer中的proxy 配置 
var request = axios.create({
    baseURL: '/ankicoo',
});

//无参数请求数据
export const getWithOutParam = (url) => {
    //添加异步处理,请求数据后将数据返回回调用的地方->
    return new Promise((resolve,reject)=>{
        request.get(url)
            .then( (result) =>{
                connectSuccess(result);
                resolve(result.data.data)
            }).catch( (error) =>{
                // console.log(error);
                reject(error)
            })
    })
};

export const get = (url) => {
    //添加异步处理,请求数据后将数据返回回调用的地方->
    return new Promise((resolve,reject)=>{
        request.get(url)
            .then( (result) =>{
                connectSuccess(result);
                resolve(result.data.data)
            }).catch( (error) =>{
                reject(error)
            })
    })
};

export const post = (url,param) => {
    //添加异步处理,请求数据后将数据返回回调用的地方->
    return new Promise((resolve,reject)=>{
        request.post(url,param)
            .then( (result) =>{
                connectSuccess(result);
                resolve(result.data.data)
            }).catch( (error) =>{
                reject(error)
            })
    })
};

export const connectSuccess =(result)=>{
    if(result.data.status===401){
        window.location.href="/";
    }
}







